/**
 * 服务端接口
 * */
import requestServer from "@/api/server/server";
import { recentlyListLS, shoppingCatLS } from "@/api/localStorage/index";
import { isLogin } from "@/common/js/auth";
import { compareAttr } from "@/common/js/util";

export default {
  /**
   * 首页
   * */
  index() {
    let url = "index/index";
    let params = null;
    return requestServer(url, params);
  },
  /*
  * 登录
  * @param phone String 登录手机号
  * @param password String 登录密码
  * */
  login(phone, password) {
    let url = "login/index";
    let params = {
      phone: phone,
      password: password
    };
    return requestServer(url, params);
  },
  /**
   * 商品列表
   * @param params.page Number 分页
   * @param params.sort Number 排序条件 0:所有
   * @param params.category Number 商品分类 0:所有
   * */
  goodsList(params) {
    let url = "goods/list";
    let p = {
      page: 1,
      sort: 0,
      category: 0
    };
    if (params) {
      params.page && (p.page = params.page);
      params.sort && (p.sort = params.sort);
      params.category && (p.category = params.category);
    }
    return requestServer(url, p);
  },
  /**
   * 商品详情
   * @param id string 商品详情
   * */
  goodsDetail(id) {
    let url = "goods/detail";
    let params = {
      id: id
    };
    return requestServer(url, params);
  },
  /**
   * 商品分类
   * */
  category() {
    let url = "product/category";
    let params = null;
    return requestServer(url, params);
  },
  /**
   * 购物车
   * */
  shoppingCat() {
    let shoppingCat = shoppingCatLS().getItem();
    if (!isLogin()) {
      return Promise.resolve({
        list: shoppingCat
      });
    }
    let url = "ShoppingCat/list";
    return new Promise((resolve, reject) => {
      requestServer(url)
        .then(rs => {
          // 如果商品发布后可以改，真实开发环境需要验证通过商品id,拉商品最新信息
          let list = compareAttr(shoppingCat, rs.list, "id");
          list = list.concat(rs.list);
          resolve({
            list: list // 拼接本地和服务器搜索词
          });
          // shoppingCatLS().clearItem(); // 清空localStore
          // localStore搜索词到同步服务端
          let url = "shopping/editor";
          requestServer(url, list)
            .then(() => {
              console.log("ok");
            })
            .catch(ms => {
              console.log(ms);
            });
        })
        .catch(ms => {
          reject(ms);
        });
    });
  },
  /*
  * 变更购物车商品（删，修，增）
  * @param goods 修改商品
  * */
  setShoppingCat(list) {
    return new Promise((resolve, reject) => {
      if (!isLogin()) {
        shoppingCatLS().editItem(list);
        resolve("ok");
      } else {
        // 登录后直接走接口（实际工作中，删、修、增需要拆分并挑选后台需要的参数，此处就偷懒了）
        let url = "shopping/editor";
        requestServer(url, list)
          .then(() => {
            resolve("ok");
          })
          .catch(ms => {
            reject(ms);
          });
      }
    });
  },
  /**
   * 搜索商品
   * @param params.query String 搜索关键字
   * @param params.page Number 分页
   * @param params.sort Object 排序条件
   * @param params.category Number 商品分类 0:所有
   * */
  search(params) {
    if (!params || !params.query) {
      return Promise.reject("搜索关键词为必须");
    }
    let url = "search/index";
    let p = {
      page: 1,
      sort: { key: "all", status: 0 },
      category: 0
    };
    params.query && (p.query = params.query);
    params.page && (p.page = params.page);
    params.sort && (p.sort = params.sort);
    params.category && (p.category = params.category);

    return requestServer(url, p);
  },
  /**
   * 搜索联想关键词
   * @param key String
   * */
  searchKey(key) {
    let url = "search/key";
    let params = {};
    key && (params.key = key);
    return requestServer(url, params);
  },
  /**
   * 猜你喜欢联想词
   * */
  searchLoveList() {
    let url = "search/loveList";
    return requestServer(url);
  },
  /**
   * 最近搜索
   * */
  searchRecentlyList() {
    let recentlyList = recentlyListLS().getItem();
    if (!isLogin()) {
      return Promise.resolve({
        recentlyList: recentlyList
      });
    }
    // 登陆
    let url = "search/recently";
    return new Promise((resolve, reject) => {
      requestServer(url)
        .then(rs => {
          let list = [];
          recentlyList.forEach(val => {
            if (rs.recentlyList.indexOf(val) === -1) {
              list.unshift(val);
            }
          });
          resolve({
            recentlyList: list.concat(rs.recentlyList) // 拼接本地和服务器搜索词
          });
          recentlyListLS().clearItem(); // 清空localStore搜索词
          // localStore搜索词到同步服务端
          let url = "search/recentlySet";
          if (!list.length) return;
          let params = {};
          params.work = list;
          requestServer(url, params)
            .then(() => {
              console.log("ok");
            })
            .catch(ms => {
              console.log(ms);
            });
        })
        .catch(ms => {
          reject(ms);
        });
    });
  },
  /**
   * 设置最近搜索词
   * @param work
   * */
  setSearchRecentlyList(work) {
    // 未登陆操作localStorage
    if (!isLogin()) {
      recentlyListLS().setItem(work);
      return Promise.resolve("ok");
    }
    let url = "search/recentlySet";
    let params = {};
    params.work = [];
    params.work.unshift(work);
    return requestServer(url, params);
  },
  /**
   * 清空
   * */
  clearSearchRecentlyList() {
    if (!isLogin()) {
      recentlyListLS().clearItem();
      return Promise.resolve("ok");
    }
    let url = "search/recentlyClear";
    return requestServer(url);
  },
  /**
   * 评论列表
   * @param id 商品id
   * */
  evaluateList(id) {
    let url = "evaluate/list";
    let params = {
      id: id
    };
    return requestServer(url, params);
  }
};
